/**
 * //给定两个整数数组a和b，计算具有最小差绝对值的一对数值（每个数组中取一个值），并返回该对数值的差
 * //
 * //
 * //
 * // 示例：
 * //
 * //
 * //输入：{1, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
 * //输出：3，即数值对(11, 8)
 * //
 * //
 * //
 * //
 * // 提示：
 * //
 * //
 * // 1 <= a.length, b.length <= 100000
 * // -2147483648 <= a[i], b[i] <= 2147483647
 * // 正确结果在区间 [0, 2147483647] 内
 * //
 * //
 * // Related Topics 数组 双指针 二分查找 排序 👍 59 👎 0
 */

package com.xixi.basicAlgroithms.dualPointer;

import java.util.Arrays;

public class ID_16_06_SmallestDifferenceLcci {
    public static void main(String[] args) {
        Solution solution = new ID_16_06_SmallestDifferenceLcci().new Solution();
    }


    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        long min = Long.MAX_VALUE;

        public int smallestDifference(int[] a, int[] b) {
            Arrays.sort(a);
            Arrays.sort(b);

            int aIndex = 0;
            int bIndex = 0;


            min = Math.abs(Math.abs((long) a[aIndex]) - Math.abs((long) b[bIndex]));

            while (aIndex < a.length && bIndex < b.length) {
                long sub = Math.abs(Math.abs((long) a[aIndex]) - Math.abs((long) b[bIndex]));
                min = Math.min(sub, min);

                if (a[aIndex] > b[bIndex]) {
                    bIndex++;
                } else if (a[aIndex] < b[bIndex]) {
                    aIndex++;
                } else {
                    return 0;
                }


            }


            return (int) min;


        }
    }
//leetcode submit region end(Prohibit modification and deletion)


}